<template>
	<!-- 我的-康复顾问-会员详情 -->
	<view class="content">
		<view class="bg"></view>
		<view style="margin-top: 20rpx;z-index: 2;">
			<view class="collapse">
				<uni-collapse ref="collapse" v-model="value" @change="change">
					<uni-collapse-item title-border="none" :border="false" open="true">
						<template v-slot:title>
							<view class="title-box">
								<image src="/static/icon/jbzl.png" class="icon"></image>
								<view class="title">基本资料</view>
							</view>
						</template>
						<view class="info-content">
							<view class="info-item">
								<view class="key">姓名</view>
								<uni-easyinput v-model="memberData.name" placeholder="请输入姓名" :styles="{borderColor: '#fff'}" />
							</view>
							<view class="info-item">
								<view class="key">身份证号</view>
								<!-- <view>{{ getAge(baseFormData.idnumber)}}</view> -->
								<uni-easyinput v-model="memberData.idnumber" placeholder="请输入身份证号" :styles="{borderColor: '#fff'}" />
							</view>
<!-- 							<view class="info-item">
								<view class="key">性别</view>
								<view>{{ memberData.gender }}</view>
							</view>
							<view class="info-item">
								<view class="key">年龄</view>
								<view>{{ getAge(memberData.idnumber) }}</view>
							</view> -->
							<view class="info-item">
								<view class="key">手机号</view>
								<uni-easyinput v-model="memberData.mobileTel" placeholder="请输入手机号" :styles="{borderColor: '#fff'}" />
							</view>
							<view class="info-item">
								<view class="key">受伤部位</view>
								<uni-data-select :clear="false" v-model="memberData.symptom" style="width:45%;" :localdata="range" @change="change">
								</uni-data-select>
							</view>
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<view class="collapse">
				<uni-collapse ref="collapse" v-model="value" @change="change">
					<uni-collapse-item title-border="none" :border="false">
						<template v-slot:title>
							<view class="title-box">
								<image src="/static/icon/jkzl.png" class="icon"></image>
								<view class="title">健康资料</view>
							</view>
						</template>
						<view class="info-content">
							<!-- 							<view class="info-item">
										<view>姓名</view>
										<view>王树林</view>
									</view>
									<view class="info-item">
										<view>性别</view>
										<view>男</view>
									</view>
									<view class="info-item">
										<view>年龄</view>
										<view>39</view>
									</view>
									<view class="info-item">
										<view>手机号</view>
										<view>1352728192</view>
									</view>
									<view class="info-item">
										<view>受伤部位</view>
										<view>小腿</view>
									</view> -->
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<view class="collapse">
				<uni-collapse ref="collapse" v-model="value" @change="change">
					<uni-collapse-item title-border="none" :border="false">
						<template v-slot:title>
							<view class="title-box">
								<image src="/static/icon/kfjh2.png" class="icon"></image>
								<view class="title">康复计划</view>
							</view>
						</template>
						<view class="info-content">
							<!-- 							<view class="info-item">
										<view>姓名</view>
										<view>王树林</view>
									</view>
									<view class="info-item">
										<view>性别</view>
										<view>男</view>
									</view>
									<view class="info-item">
										<view>年龄</view>
										<view>39</view>
									</view>
									<view class="info-item">
										<view>手机号</view>
										<view>1352728192</view>
									</view>
									<view class="info-item">
										<view>受伤部位</view>
										<view>小腿</view>
									</view> -->
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<view class="collapse">
				<uni-collapse ref="collapse" v-model="value" @change="change">
					<uni-collapse-item title-border="none" :border="false">
						<template v-slot:title>
							<view class="title-box">
								<image src="/static/icon/kfpg.png" class="icon"></image>
								<view class="title">康复评估</view>
							</view>
						</template>
						<view class="info-content">
							<!-- 							<view class="info-item">
										<view>姓名</view>
										<view>王树林</view>
									</view>
									<view class="info-item">
										<view>性别</view>
										<view>男</view>
									</view>
									<view class="info-item">
										<view>年龄</view>
										<view>39</view>
									</view>
									<view class="info-item">
										<view>手机号</view>
										<view>1352728192</view>
									</view>
									<view class="info-item">
										<view>受伤部位</view>
										<view>小腿</view>
									</view> -->
						</view>
					</uni-collapse-item>

				</uni-collapse>
			</view>
			<view class="collapse" style="margin-bottom: 180rpx;">
				<uni-collapse ref="collapse" v-model="value" @change="change">
					<uni-collapse-item title-border="none" :border="false">
						<template v-slot:title>
							<view class="title-box">
								<image src="/static/icon/ysjy.png" class="icon"></image>
								<view class="title">医生建议</view>
							</view>
						</template>
						<view class="info-content">
							<!-- 							<view class="info-item">
										<view>姓名</view>
										<view>王树林</view>
									</view>
									<view class="info-item">
										<view>性别</view>
										<view>男</view>
									</view>
									<view class="info-item">
										<view>年龄</view>
										<view>39</view>
									</view>
									<view class="info-item">
										<view>手机号</view>
										<view>1352728192</view>
									</view>
									<view class="info-item">
										<view>受伤部位</view>
										<view>小腿</view>
									</view> -->
						</view>
					</uni-collapse-item>
				</uni-collapse>
			</view>
			<view style="display: flex;left:0; center;justify-content: center;position: fixed;
					bottom: 0rpx;">
				<view class="btn-cancel" @click="cancel()">取消</view>
				<view class="btn-save" @click="save()">保存</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				range: [{
						value: 1,
						text: "全髋"
					},
					{
						value: 2,
						text: "全膝"
					},
					{
						value: 3,
						text: "全肩"
					},
					{
						value: 4,
						text: "全肘"
					},
					{
						value: 5,
						text: "掌指"
					},
					{
						value: 6,
						text: "髋部"
					}
				],
				isedit: false,
				memberData: {}
			}
		},
		onLoad(option) {
		},
		mounted() {},
		methods: {
			validateIdCard(idCard) {
				// 只包含数字、大小写字母和xX字符，并且长度为15或18位
				const regExp = /^[0-9a-zA-Z]{15}$|^[0-9a-zA-Z]{18}$|^[0-9a-zA-Z]{17}[xX]$/;

				if (!regExp.test(idCard)) {
					return false;
				}

				// 校验省份信息，只能是已知的省份
				const provinceCode = idCard.substring(0, 2);
				const provinceList = ['11', '12', '13', '14', '15', '21', '22', '23', '31', '32', '33', '34', '35', '36', '37', '41', '42', '43', '44', '45', '46', '50', '51', '52', '53', '54', '61', '62', '63', '64', '65', '71', '81', '82'];
				if (!provinceList.includes(provinceCode)) {
					return false;
				}

				// 校验出生日期信息，需满足年月日格式以及合法性
				const birthStr = idCard.substring(6, 14);
				const year = birthStr.substring(0, 4);
				const month = birthStr.substring(4, 6);
				const day = birthStr.substring(6, 8);
				const birthDate = new Date(`${year}-${month}-${day}`);
				if (birthDate.getFullYear() !== parseInt(year) || birthDate.getMonth() + 1 !== parseInt(month) || birthDate.getDate() !== parseInt(day)) {
					return false;
				}

				// 校验校验码信息，需满足规则
				const checkCode = idCard.substring(17, 18);
				let sum = 0;
				const coefficientList = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
				const checkCodeList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
				for (let i = 0; i < 17; i++) {
					sum += parseInt(idCard.charAt(i)) * coefficientList[i];
				}
				const modValue = sum % 11;
				return checkCode.toUpperCase() === checkCodeList[modValue];
			},
			getAge(idCard) {
				if (idCard == null || idCard == "") {
					return 0;
				}
				// 获取出生年月日
				const birthYear = idCard.substr(6, 4);
				const birthMonth = idCard.substr(10, 2);
				const birthDay = idCard.substr(12, 2);
			
				// 构造出生日期对象
				const birthDate = new Date(`${birthYear}-${birthMonth}-${birthDay}`);
			
				// 获取当前日期对象
				const currentDate = new Date();
			
				// 计算出生日期到当前日期的毫秒数
				const diffTime = currentDate.getTime() - birthDate.getTime();
			
				// 转换为年龄并返回
				return Math.floor(diffTime / (365 * 24 * 60 * 60 * 1000));
			},
			getBirthdayByIdCard(idCard) {
				if (idCard == null || idCard == "") {
					return "";
				}
				// 获取出生年月日
				const birthYear = idCard.substr(6, 4);
				const birthMonth = idCard.substr(10, 2);
				const birthDay = idCard.substr(12, 2);
			
				// 构造出生日期对象
				const birthDate = birthYear + "-" + birthMonth + "-" + birthDay;
			
				// 转换为年龄并返回
				return birthDate;
			},
			getGenderByIdCard(idCard) {
			  let genderCode = idCard.substring(16, 17);
			  return parseInt(genderCode) % 2 === 1 ? '男' : '女';
			},
			edit() {
				this.isedit = true
			},
			cancel() {
				uni.navigateBack({
					delta: 1
				});
			},
			save() {
				let that = this;
				if (that.memberData.name == null || that.memberData.name == "") {
					uni.showModal({
						content: '请正确填写姓名',
						showCancel: false,
						buttonText: '确定',
					})
				} else if (that.memberData.mobileTel == null || that.memberData.mobileTel  == "") {
					uni.showModal({
						content: '请正确填写手机号',
						showCancel: false,
						buttonText: '确定',
					})
				} else if (that.memberData.symptom == null || that.memberData.symptom  == "") {
					uni.showModal({
						content: '请正确选择受伤部位',
						showCancel: false,
						buttonText: '确定',
					})
				} else if (that.memberData.idnumber == null || that.memberData.idnumber == "" || !that.validateIdCard(that.memberData.idnumber)) {
					uni.showModal({
						content: '请正确填写身份证',
						showCancel: false,
						buttonText: '确定',
					})
				}
				that.memberData.age = that.getAge(that.memberData.idnumber);
				that.memberData.gender = that.getGenderByIdCard(that.memberData.idnumber);
				that.memberData.birthdate = that.getBirthdayByIdCard(that.memberData.idnumber);
				that.memberData.recureAdviserId = getApp().globalData.recureAdviserId;
				that.memberData.recureAdviserName = getApp().globalData.adviserName;
				that.memberData.mainPhoto = "/static/logo.png";
				
				uni.request({
					url: "https://guokang365.mordo-care.com/api/member/create",
					method: 'POST',
					//仅为示例，并非真实接口地址。
					header: {
						'Cookie': "JSESSIONID=" + getApp().globalData.serverSession
						// 'JSESSIONID': getApp().globalData.serverSession //自定义请求头信息
					},
					data: that.memberData,
					success: (res) => {
						// todo 需要使用级联查询，查套餐名
						if (res.data.result != '100') {
							uni.showModal({
								content: '操作失败',
								showCancel: false,
								buttonText: '确定',
							})

						} else {
							uni.showModal({
								content: '操作成功',
								showCancel: false,
								buttonText: '确定',
							})
							that.returnLastPage();
						}
					},
				});
			},
			returnLastPage() {
				//获取页面栈数据
				const pages = getCurrentPages()
				//当长度为1时，则是打开小程序时首先进来的页面，没有上一页
				if (pages.length <= 1) {
					// uni.switchTab({
					// 	url: '/pages/home/home'
					// })
				} else {
					//存上一页信息
					let prevPage = ''
					//存上一页完整路由
					let prevRoute = ''
					//pages的长度-2为上一页，pages最后一个元素则是当前页
					if (pages.length >= 2) {
						prevPage = pages[pages.length - 2]
						prevRoute = prevPage.route
					}
					//返回上一页并触发上一页的刷新方法，加个定时器的原因：降低返回时调用刷新函数失败几率
					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
							success: (event) => {
								//判断是否为刷新数据的目标页
								if (prevRoute.indexOf('pages/my/custom-list') > -1) {
									prevPage.$vm.reLoadFn()
								}
							}
						});
					}, 300)
				}
			},
			getSymptomText(value) {
				switch (value) {
					case 1:
						return '全髋'
					case 2:
						return '全膝'
					case 3:
						return '全肩'
					case 4:
						return '全肘'
					case 5:
						return '掌指'
					case 6:
						return '髋部'
					default:
						return ''
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.bg {
		width: 750rpx;
		height: 128rpx;
		background: #CBFFDB;
		filter: blur(25px);
		position: absolute;
		z-index: 1;
		top: 0px;
	}

	.key {
		width: 360rpx;
	}

	.btn-cancel {
		width: 375rpx;
		height: 129rpx;
		background: #D1D1D1;
		font-size: 30rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #525252;
		line-height: 129rpx;
		text-align: center;
	}

	.btn-save {
		width: 375rpx;
		height: 129rpx;
		background: #42AE49;
		font-size: 30rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 120rpx;
		text-align: center;
	}

	.collapse {
		width: 707rpx;
		background: #FFFFFF;
		border-radius: 22rpx;
		box-sizing: border-box;
		padding: 20rpx 0rpx;
		margin-top: 10rpx;

		.title-box {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			margin: 10rpx 30rpx;

			.icon {
				width: 43rpx;
				height: 43rpx;
				margin-right: 20rpx;
			}

			.title {
				font-size: 29rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 500;
				color: #484848;
				line-height: 40rpx;
			}
		}

		.info-content {
			box-sizing: border-box;
			width: 707rpx;
			padding: 0 34rpx;
		}

		.info-item {
			justify-content: space-between;
			flex-direction: row;
			display: flex;
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #393939;
			line-height: 85rpx;
		}
	}

	.btn {
		position: fixed;
		bottom: 0rpx;
		text-align: center;
		width: 750rpx;
		height: 129rpx;
		background: #42AE49;
		font-size: 30rpx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 129rpx;
		z-index: 1000;
		left: 0;

	}

	::v-deep .uni-collapse-item__wrap {
		overflow: inherit !important;
	}
</style>